<?php

namespace App\Models\Shop;

use App\Models\User\WxUser;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxShopGoods extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'wx_shop_goods';

    public function searchableAs()
    {
        return str_replace('.', '_', str_replace(['https://', 'http://'], '', env('APP_URL', 'minisns.cn'))).'_'.'wx_shop_goods_index';
    }
    /**
     * 获取模型的可索引的数据。
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();
        $search_data = [
            'id' => $array['id'],
            'type' => $array['type'],
            'name' => $array['name'],
            'intro' => $array['intro'],
            'content' => strip_tags($array['content']),
            'state' => $array['state'],
            'buys' => $array['buys'],
            'pic' => $array['pic'],
            'price' => $array['price'],
            'vip_price' => $array['vip_price'],
            'credit_type' => $array['credit_type'],
        ];

        return $search_data;
    }

    /**
     * 确定模型是否可搜索
     *
     * @return bool
     */
    public function shouldBeSearchable()
    {
        return ($this->state == 1);
    }


    public function user()
    {
        return $this->hasOne(WxUser::class,'id','user_id');
    }

    public function classify(){
        return $this->hasOne(WxShopClassify::class,'id','classify_id');
    }



    public function product(){
        return $this->hasMany(WxShopGoodsProduct::class,'goods_id','id');
    }

    public function getPriceAttribute($v){
        if($this->credit_type == 1){
            return $v;
        }else{
            return (int)$v;
        }
    }

    public function getVipPriceAttribute($v){
        if($this->credit_type == 1){
            return $v;
        }else{
            return (int)$v;
        }
    }


    public function getPicAttribute($pictures)
    {
        return explode(',',$pictures);
    }


    public function getTagAttribute($a){
        $arr = json_decode($a,false);
        return $arr;
    }

    /**
     * 根据规格获取价格
     */
    public function getProductPrice(){

    }


    public function getTkPlatformIdAttribute($value){
        if(_empty_($value)){
            return '';
        }
        return $value;
    }


}
